<?php
namespace Home\Controller;
use Think\Controller;
/*
 用户活动
*/
class ActivityController extends Controller {
	
	public function index()
	{		   
			$this->display();
	}
	//活动表示
	public function activity()
	{
		 $activity_type=$_GET['activity_type'];
		 $user_id = session('id');
        /*  dump($user_id);
         return;  */		 
		 $actInfo=M('activity_table');
		 $gymInfo=M('gymnasium_table');
		 $cocahInfo=M('coach_table');
		 if ($activity_type==0)
		 {
		 $this->assign('activity_type',0);
		 $act_info=$actInfo->join('thinkphp_gymnasium_table on thinkphp_activity_table.activity_gymnasium=thinkphp_gymnasium_table.gymnasium_id')->join('thinkphp_user_table on thinkphp_activity_table.activity_organizer_id=thinkphp_user_table.id')->where('activity_status=1')->order('activity_id desc')->field('activity_id,activity_name,activity_brief,gymnasium_name,apply_number,apply_start_time,apply_end_time,activity_start_time,activity_end_time,name,activity_content,max_apply_number,activity_picture')->select();
	     }
		 else if ($activity_type==1)
		 {
	     $this->assign('activity_type',1);
		 $act_info=$actInfo->join('thinkphp_gymnasium_table on thinkphp_activity_table.activity_gymnasium=thinkphp_gymnasium_table.gymnasium_id')->join('thinkphp_user_table on thinkphp_activity_table.activity_organizer_id=thinkphp_user_table.id')->join('thinkphp_apply_table on thinkphp_activity_table.activity_id=thinkphp_apply_table.apply_activity_id')->where("apply_user_id='%s' and activity_status='%s'",array($user_id,1))->order('activity_id desc')->field('activity_id,activity_name,activity_brief,gymnasium_name,apply_number,apply_start_time,apply_end_time,activity_start_time,activity_end_time,name,activity_content,max_apply_number,activity_picture')->select();	
		 }
		 $this->assign('act_info',json_encode($act_info));
		 $this->display('index');
	}
	//查询活动
	public function search()
	{
		
		$search_type=$_POST['search_type'];
		$search_content=$_POST['search_keyword'];
        $activity_type=$_POST['activity_type'];
		 $user_id = session('id');
		 $status=1;
		 $actInfo=M('activity_table');
		 if ($search_type==1) 
		 {
		   if   ($activity_type==0)
		   $act_info=$actInfo->join('thinkphp_gymnasium_table on thinkphp_activity_table.activity_gymnasium=thinkphp_gymnasium_table.gymnasium_id')->join('thinkphp_user_table on thinkphp_activity_table.activity_organizer_id=thinkphp_user_table.id')->where("activity_status='%d' and gymnasium_name='%s'",array($status,$search_content))->order('activity_id desc')->field('activity_id,activity_name,activity_brief,gymnasium_name,apply_number,apply_start_time,apply_end_time,activity_start_time,activity_end_time,name,activity_content,max_apply_number,activity_picture')->limit(100)->select();
	       else  if   ($activity_type==1)
		   $act_info=$actInfo->join('thinkphp_gymnasium_table on thinkphp_activity_table.activity_gymnasium=thinkphp_gymnasium_table.gymnasium_id')->join('thinkphp_user_table on thinkphp_activity_table.activity_organizer_id=thinkphp_user_table.id')->join('thinkphp_apply_table on thinkphp_activity_table.activity_id=thinkphp_apply_table.apply_activity_id')->where("apply_user_id='%s' and activity_status='%s' and gymnasium_name='%s'",array($user_id,1,$search_content))->order('activity_id desc')->field('activity_id,activity_name,activity_brief,gymnasium_name,apply_number,apply_start_time,apply_end_time,activity_start_time,activity_end_time,name,activity_content,max_apply_number,activity_picture')->limit(100)->select();		   
		 }
		 else if ($search_type==2) 
		 {
		   if   ($activity_type==0)
		   $act_info=$actInfo->join('thinkphp_gymnasium_table on thinkphp_activity_table.activity_gymnasium=thinkphp_gymnasium_table.gymnasium_id')->join('thinkphp_user_table on thinkphp_activity_table.activity_organizer_id=thinkphp_user_table.id')->where("activity_status='%d' and name='%s'",array($status,$search_content))->order('activity_id desc')->field('activity_id,activity_name,activity_brief,gymnasium_name,apply_number,apply_start_time,apply_end_time,activity_start_time,activity_end_time,name,activity_content,max_apply_number,activity_picture,activity_picture')->select();
	       else  if   ($activity_type==1)
		   $act_info=$actInfo->join('thinkphp_gymnasium_table on thinkphp_activity_table.activity_gymnasium=thinkphp_gymnasium_table.gymnasium_id')->join('thinkphp_user_table on thinkphp_activity_table.activity_organizer_id=thinkphp_user_table.id')->join('thinkphp_apply_table on thinkphp_activity_table.activity_id=thinkphp_apply_table.apply_activity_id')->where("apply_user_id='%s' and activity_status='%s' and name='%s'",array($user_id,1,$search_content))->order('activity_id desc')->field('activity_id,activity_name,activity_brief,gymnasium_name,apply_number,apply_start_time,apply_end_time,activity_start_time,activity_end_time,name,activity_content,max_apply_number,activity_picture')->limit(100)->select();		   
		 }
		 else if ($search_type==3)
		 {
		   if   ($activity_type==0)
		   $act_info=$actInfo->join('thinkphp_gymnasium_table on thinkphp_activity_table.activity_gymnasium=thinkphp_gymnasium_table.gymnasium_id')->join('thinkphp_user_table on thinkphp_activity_table.activity_organizer_id=thinkphp_user_table.id')->where("activity_status='%d' and activity_name='%s'",array($status,$search_content))->order('activity_id desc')->field('activity_id,activity_name,activity_brief,gymnasium_name,apply_number,apply_start_time,apply_end_time,activity_start_time,activity_end_time,name,activity_content,max_apply_number,activity_picture')->select();
	       else  if   ($activity_type==1)
		   $act_info=$actInfo->join('thinkphp_gymnasium_table on thinkphp_activity_table.activity_gymnasium=thinkphp_gymnasium_table.gymnasium_id')->join('thinkphp_user_table on thinkphp_activity_table.activity_organizer_id=thinkphp_user_table.id')->join('thinkphp_apply_table on thinkphp_activity_table.activity_id=thinkphp_apply_table.apply_activity_id')->where("apply_user_id='%s' and activity_status='%s' and activity_name='%s'",array($user_id,1,$search_content))->order('activity_id desc')->field('activity_id,activity_name,activity_brief,gymnasium_name,apply_number,apply_start_time,apply_end_time,activity_start_time,activity_end_time,name,activity_content,max_apply_number,activity_picture')->limit(100)->select();		   
		 
		 }
		/*  dump($act_info);
		 return; */ 
		 $this->assign('act_info',json_encode($act_info));
		 $this->display('index');
	}
	//报名
	public function regis()
	{
		$actInfo=M('activity_table');
		$applyInfo=M('apply_table');
		$user_id = session('id');
		
		/*  dump($user_id);
		return;  */
		$activity_id=$_POST['act_id'];
		
		$is_apply=$applyInfo->where("apply_user_id='%s' and apply_activity_id='%s' and activity_type='%s' ",array($user_id,$activity_id,0))->select();
		if (!$is_apply) 
		{
		   if ($user_id)
		   {
		   $time=$actInfo->where("activity_id='%d'",$activity_id)->getField('apply_end_time');   
		   $now_apply_number=$actInfo->where("activity_id='%d'",$activity_id)->getField('apply_number');
		   $max_apply_number=$actInfo->where("activity_id='%d'",$activity_id)->getField('max_apply_number');
		   if (strtotime($time)<time())  
		   {
			   //$resultList['msg']=strtotime($time)<time();
			   $resultList['msg']="报名时间已过，请选择其他活动";
			   $apply_number=$actInfo->where("activity_id=%s",$activity_id)->getFIeld('apply_number');
			   $resultList['apply_number']=$apply_number;
		   }
	       else
		   if ($max_apply_number==$now_apply_number )
           {
			   $resultList['msg']="报名人数已满，请选择其他活动";
			   $apply_number=$actInfo->where("activity_id=%s",$activity_id)->getFIeld('apply_number');
			   $resultList['apply_number']=$apply_number;
		   }
           else 		   
		   {
		  
		   $data['apply_time']=date("y-m-d H:i:s");
		   $data['apply_user_id']=$user_id;
		   $data['apply_activity_id']=$activity_id;
		   $data['activity_type']=0;
		   $applyInfo->add($data);
		    $resultList['msg']="报名成功";
		   $apply_number=$actInfo->where("activity_id=%s",$activity_id)->getFIeld('apply_number');
		   $actvity['apply_number']= $apply_number+1;
		   $actInfo->where("activity_id=%s",$activity_id)->save($actvity);
		   $resultList['apply_number']=$actvity['apply_number'];
		   }
		   }
           else	
		   {
			   $apply_number=$actInfo->where("activity_id=%s",$activity_id)->getFIeld('apply_number');
			   $resultList['apply_number']=$apply_number;
			   $resultList['msg']="请先登录";
		   }			   
		}
		else
		{
			$apply_number=$actInfo->where("activity_id=%s",$activity_id)->getFIeld('apply_number');
			$actvity['apply_number']= $apply_number;
			$resultList['apply_number']=$actvity['apply_number'];
			$resultList['msg'] = "你已报名，无需重复报名";
		}
		/* dump($resultList);
		return;  */
		$this->ajaxReturn($resultList);
	}
	//创建活动html
	public function add_coach_activity()
	{
        $user=$_SESSION['think_'];
        $this->assign('user',$user);
		//设置当前的选项位置（即该选项为橙色)   
        $this->assign("biz_item_num",3);
    	$this->display();
	}
	//创建教练活动handler
	public function add_coach_activity_handler()
	{
		$_validate = array(
        	array('activity_name', 'require', '活动名称不能为空！'), 
            array('activity_apply_time', 'require', '报名开始时间不能为空！'),
			array('activity_deadline_registration', 'require', '报名结束时间不能为空！'),
			array('activity_begin_time', 'require', '活动开始时间不能为空！'),
			array('activity_deadline', 'require', '活动结束时间不能为空！'),
        	array('activity_brief', 'require', '活动简介不能为空！'), 
			array('activity_content', 'require', '活动描述不能为空！'), 
			array('activity_gymnasium', 'require', '活动举办场地不能为空！'), 
			array('is_public','require','必须选择该活动是否向全体用户公开!'),
    	);
		if (IS_POST) {
			if (!session('id')) $this->error("请先登录");
		  $upload = new \Org\Net\UploadFile();  
          $upload->maxSize = 3145728;  
          //设置文件上传类型  
          $upload->allowExts = array('jpg','gif','png','jpeg');  
          //设置文件上传位置  
          //$upload->savePath = __PUBLIC__."/Img/Uploads/";
          $upload->savePath = "./Public/Img/Uploads/activityImg/";  
          //设置文件上传名(按照时间)  
          $upload->saveRule = "time";  
          if (!$upload->upload()){  
              $this->error($upload->getErrorMsg());  
          }else{  
              //上传成功，获取上传信息  
              $info = $upload->getuploadFileInfo();
              			  
          }
          $savename = $info[0]['savename'];
         $imgurl = "/yuleclub/Public/Img/Uploads/activityImg/".$savename; 
		$activity=M('activity_table');
		$data["activity_name"] = $_POST["activity_name"];
        $data["activity_gymnasium"] = $_POST["activity_gymnasium"];
	    $data["activity_content"] = $_POST["activity_content"];
		$data["activity_brief"]=$_POST["activity_brief"];
		$data["max_apply_number"]=$_POST["activity_contain_number"];
		$data["activity_end_time"] = $_POST['activity_deadline'];
		$data["activity_start_time"]=$_POST["activity_begin_time"];
        $data["apply_end_time"] = $_POST["activity_deadline_registration"];	
		$data["apply_start_time"]=$_POST["activity_apply_time"];
		$data["activity_type"]=2;
		$data["activity_if_public"]=1;
		$data["apply_number"]=0;
		$data["activity_status"]=1;//need change after adding admin account
		$data['activity_picture']=$imgurl;
		$data["activity_organizer_id"]=session('id');;
			if (!$result = $activity->validate($_validate)->create())
			{
				
				$this->error($activity->getError());
			}
			else
			if ($activity->add($data))
			{
				$coach_activity=M('activity_coach_table');
				$coach_info['a_activity_id']=$activity->order('activity_id desc')->limit(1)->getField('activity_id');
				$coach_info['a_coach_id']=session('id');
				
				$coach_activity->add($coach_info);
				$this->success("添加成功");
			}
			else
			{
				/* dump($activity->getLastSql());
				return; */
				$this->error("请正确输入球馆id");
			}
		}
	}
	//创建球馆活动handler
	public function add_gymnasium_activity_handler()
	{
		$_validate = array(
        	array('activity_name', 'require', '活动名称不能为空！'), 
            array('activity_apply_time', 'require', '报名开始时间不能为空！'),
			array('activity_deadline_registration', 'require', '报名结束时间不能为空！'),
			array('activity_begin_time', 'require', '活动开始时间不能为空！'),
			array('activity_deadline', 'require', '活动结束时间不能为空！'),
        	array('activity_brief', 'require', '活动简介不能为空！'), 
			array('activity_content', 'require', '活动描述不能为空！'), 
			array('activity_gymnasium', 'require', '活动举办场地不能为空！'), 
			array('is_public','require','必须选择该活动是否向全体用户公开!'),
    	);
		if (IS_POST) {
			if (!session('id')) $this->error("请先登录");
		  $upload = new \Org\Net\UploadFile();  
          $upload->maxSize = 3145728;  
          //设置文件上传类型  
          $upload->allowExts = array('jpg','gif','png','jpeg');  
          //设置文件上传位置  
          //$upload->savePath = __PUBLIC__."/Img/Uploads/";
          $upload->savePath = "./Public/Img/Uploads/activityImg/";  
          //设置文件上传名(按照时间)  
          $upload->saveRule = "time";  
          if (!$upload->upload()){  
              $this->error($upload->getErrorMsg());  
          }else{  
              //上传成功，获取上传信息  
              $info = $upload->getuploadFileInfo();
              			  
          }
          $savename = $info[0]['savename'];
         $imgurl = "/yuleclub/Public/Img/Uploads/activityImg/".$savename; 
		$activity=M('activity_table');
		$data["activity_name"] = $_POST["activity_name"];
        $data["activity_gymnasium"] = $_POST["activity_gymnasium"];
	    $data["activity_content"] = $_POST["activity_content"];
		$data["activity_brief"]=$_POST["activity_brief"];
		$data["max_apply_number"]=$_POST["activity_contain_number"];
		$data["activity_end_time"] = $_POST['activity_deadline'];
		$data["activity_start_time"]=$_POST["activity_begin_time"];
        $data["apply_end_time"] = $_POST["activity_deadline_registration"];	
		$data["apply_start_time"]=$_POST["activity_apply_time"];
		$data["activity_type"]=1;
		$data["activity_if_public"]=1;
		$data["apply_number"]=0;
		$data["activity_status"]=1;//need change after  adding  admin account
		$data['activity_picture']=$imgurl;
		$data["activity_organizer_id"]=session('id');;
			if (!$result = $activity->validate($_validate)->create())
			{
				
				$this->error($activity->getError());
			}
			else
			if ($activity->add($data))
			{
				$gymnasium_activity=M('activity_gymnasium_table');
				$gymnasium_info['a_activity_id']=$activity->order('activity_id desc')->limit(1)->getField('activity_id');
				$gymnasium_info['a_gymnasium_id']=$_POST['gymnasium_id'];
			
				$gymnasium_activity->add($gymnasium_info);
				$this->success("添加成功");
			}
			else
			{
				/* dump($activity->getLastSql());
				return; */
				$this->error("请正确输入球馆id");
			}
		}
	}
	//创建球会活动handler
	public function add_club_activity_handler()
	{
		$_validate = array(
        	array('activity_name', 'require', '活动名称不能为空！'), 
            array('activity_apply_time', 'require', '报名开始时间不能为空！'),
			array('activity_deadline_registration', 'require', '报名结束时间不能为空！'),
			array('activity_begin_time', 'require', '活动开始时间不能为空！'),
			array('activity_deadline', 'require', '活动结束时间不能为空！'),
        	array('activity_brief', 'require', '活动简介不能为空！'), 
			array('activity_content', 'require', '活动描述不能为空！'), 
			array('activity_gymnasium', 'require', '活动举办场地不能为空！'), 
			array('is_public','require','必须选择该活动是否向全体用户公开!'),
    	);
		if (IS_POST) {
			if (!session('id')) $this->error("请先登录");
		  $upload = new \Org\Net\UploadFile();  
          $upload->maxSize = 3145728;  
          //设置文件上传类型  
          $upload->allowExts = array('jpg','gif','png','jpeg');  
          //设置文件上传位置  
          //$upload->savePath = __PUBLIC__."/Img/Uploads/";
          $upload->savePath = "./Public/Img/Uploads/activityImg/";  
          //设置文件上传名(按照时间)  
          $upload->saveRule = "time";  
          if (!$upload->upload()){  
              $this->error($upload->getErrorMsg());  
          }else{  
              //上传成功，获取上传信息  
              $info = $upload->getuploadFileInfo();
              			  
          }
          $savename = $info[0]['savename'];
         $imgurl = "/yuleclub/Public/Img/Uploads/activityImg/".$savename; 
		$activity=M('activity_table');
		$data["activity_name"] = $_POST["activity_name"];
        $data["activity_gymnasium"] = $_POST["activity_gymnasium"];
	    $data["activity_content"] = $_POST["activity_content"];
		$data["activity_brief"]=$_POST["activity_brief"];
		$data["max_apply_number"]=$_POST["activity_contain_number"];
		$data["activity_end_time"] = $_POST['activity_deadline'];
		$data["activity_start_time"]=$_POST["activity_begin_time"];
        $data["apply_end_time"] = $_POST["activity_deadline_registration"];	
		$data["apply_start_time"]=$_POST["activity_apply_time"];
		$data["activity_if_public"]=$_POST["is_public"];
		$data["activity_type"]=3;
		$data["apply_number"]=0;
		$data["activity_status"]=1;//need change after  adding  admin account
		$data['activity_picture']=$imgurl;
		$data["activity_organizer_id"]=session('id');;
			if (!$result = $activity->validate($_validate)->create())
			{
				
				$this->error($activity->getError());
			}
			else
			if ($activity->add($data))
			{
				$club_activity=M('activity_club_table');
				$club_info['activity_id']=$activity->order('activity_id desc')->limit(1)->getField('activity_id');
				$club_info['club_id']=$_POST['club_id'];
			
				$club_activity->add($club_info);
				$this->success("添加成功");
			}
			else
			{
				/* dump($activity->getLastSql());
				return; */
				$this->error("请正确输入球馆id");
			}
		}
	}
	//发表活动一览
	public function user_activity_list()
	{
		//加载当前用户信息
		$user_id=session('id');
		$user=$_SESSION['think_'];
        $this->assign('user',$user);
        //设置当前的选项位置（即该选项为橙色)
        $this->assign("biz_item_num",4);	
	    $activity=M('activity_table');
    	$list = $activity->where("activity_organizer_id=%d",$user_id)->select();
    	$this->data = $list; 
    	$this->display(); 
	}
	//删除活动
	public function deleteActivity()
	{
		$num=$_POST['act_id'];
		$user_id=session('id');
		$activity=M('activity_table');
		$list=$activity->where("activity_organizer_id=%d",$user_id)->select();
		$act_id=$list[$num][activity_id];
		$status=$activity->where("activity_id='%d'",$act_id)->getField('activity_status');
		if ($status==0)
		{
		   $activity->where("activity_id='%d'",$act_id)->delete();
		   $resultList['msg'] = $status;
		}
		else
		{
			$resultList['msg'] = $status;
		}
		$this->ajaxReturn($resultList);
	}
	//活动详情
	public function activityDetail()
	{
		$num=$_POST['act_id'];
		$user_id=session('id');
		$activity=M('activity_table');
		$list=$activity->where("activity_organizer_id=%d",$user_id)->select();
		$act_id=$list[$num][activity_id];
		$status=$activity->where("activity_id='%d'",$act_id)->getField('activity_status');
		$resultList["result"]=$list[$num];
		$resultList['msg'] = $status;
		$this->ajaxReturn($resultList);
	}
	//修改活动
	public function updateActivity()
	{
		
		if (IS_POST) {
		  if (!session('id')) $this->error("请先登录");
		  $upload = new \Org\Net\UploadFile();  
          $upload->maxSize = 3145728;  
          //设置文件上传类型  
          $upload->allowExts = array('jpg','gif','png','jpeg');  
          //设置文件上传位置  
          //$upload->savePath = __PUBLIC__."/Img/Uploads/";
          $upload->savePath = "./Public/Img/Uploads/";  
          //设置文件上传名(按照时间)  
          $upload->saveRule = "time";  
          if (!$upload->upload()){  
          }else{  
              //上传成功，获取上传信息  
              $info = $upload->getuploadFileInfo();
              $savename = $info[0]['savename'];
              $imgurl = "/yuleclub/Public/Img/Uploads/".$savename;
              $data['activity_picture']=$imgurl;			  
          }
          
		$activity=M('activity_table');
		$activity_id = $_POST["activity_id"];
		if  ($_POST["activity_name"]!=NULL)
		$data["activity_name"] = $_POST["activity_name"];
	    if  ($_POST["activity_gymnasium"]!=NULL)
        $data["activity_gymnasium"] = $_POST["activity_gymnasium"];
	    if  ($_POST["activity_content"]!=NULL)
	    $data["activity_content"] = $_POST["activity_content"];
	    if  ($_POST["activity_brief"]!=NULL)
		$data["activity_brief"]=$_POST["activity_brief"];
	    if  ($_POST["activity_contain_number"]!=NULL)
		$data["max_apply_number"]=$_POST["activity_contain_number"];
	    if  ($_POST["activity_deadline"]!=NULL)
		$data["activity_end_time"] = $_POST['activity_deadline'];
	    if  ($_POST["activity_begin_time"]!=NULL)
		$data["activity_start_time"]=$_POST["activity_begin_time"];
	    if  ($_POST["activity_deadline_registration"]!=NULL)
        $data["apply_end_time"] = $_POST["activity_deadline_registration"];
        if  ($_POST["activity_apply_time"]!=NULL)	
		$data["apply_start_time"]=$_POST["activity_apply_time"];
	    if  ($_POST["is_public"]!=NULL)
		$data["activity_if_public"]=$_POST["is_public"];
			if ($activity->where("activity_id='%d'",$activity_id)->save($data))
			{
				$this->success("修改成功");
			}
			
		} 
	} 
	//查看报名的人
	public function applyPeople()
	{
		$num=$_POST['act_id'];
		$user_id=session('id');
		$activity=M('activity_table');
		$apply=M('apply_table');
		$list=$activity->where("activity_organizer_id=%d",$user_id)->select();
		$act_id=$list[$num][activity_id];
		$people=$apply->join('thinkphp_user_table on thinkphp_apply_table.apply_user_id=thinkphp_user_table.id')->where("apply_activity_id='%d'",$act_id)->field('name')->select();
		$resultList["result"]=$people;
		$this->ajaxReturn($resultList);
	}
	//活动类型切换（管理端）
	public function changeType()
	{
		$user=$_SESSION['think_'];
        $this->assign('user',$user);
		$select_activity=$_POST["activity_select"];
		$user_id=session('id');
		$this->assign("biz_item_num",4);	
	    $activity=M('activity_table');	
    	$list = $activity->where("activity_type='%d' and activity_organizer_id='%d'",array($select_activity,$user_id))->select();
		$this->data = $list;	
		$this->display('user_activity_list');
	}
}
